package testing.domain.person.dao;

import testing.dataBase.DataBaseConnectionPool;
import testing.domain.person.Administrator;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;


public class AdministratorDao {
    public int createAdministrator (Administrator administrator) throws SQLException {
        Connection conn = null;
        PreparedStatement prepStat = null;
        try {
            conn = DataBaseConnectionPool.getConnection();
            prepStat = conn.prepareStatement("INSERT INTO TUTOR (TUTOR_ID,NAME,SURNAME,PASSWORD) VALUES ( ?, ?, ?, ?);");
            //
            ResultSet resultSet = prepStat.executeQuery("select * from TUTOR");
            int colCount = resultSet.getMetaData().getColumnCount();
            //
            colCount++;
            prepStat.setInt(1, colCount);
            prepStat.setString(2, administrator.getName());
            prepStat.setString(3, administrator.getSurname());
            prepStat.setString(4, administrator.getPassword());

            return prepStat.executeUpdate();
        }finally {
            DataBaseConnectionPool.silentClose(prepStat);
            DataBaseConnectionPool.silentClose(conn);
        }
    }
    public java.util.Collection<Administrator> getAllAdministrators() throws SQLException {
        List<Administrator> list = new ArrayList<Administrator>();
        Connection conn = null;
        PreparedStatement prepStat = null;
        try {
            conn = DataBaseConnectionPool.getConnection();
            Statement statement = conn.createStatement();
            ResultSet resultSet = statement.executeQuery("SELECT * FROM TUTOR;");
            while(resultSet.next())
            {
                list.add(
                        new Administrator(
                            resultSet.getInt("TUTOR_ID"),
                            resultSet.getString("NAME"),
                            resultSet.getString("SURNAME"),
                            resultSet.getString("PASSWORD"))
                );
            }
            }finally {
            DataBaseConnectionPool.silentClose(prepStat);
            DataBaseConnectionPool.silentClose(conn);
        }
        return list;

    }
     public int editAdministrator(Administrator administrator) throws SQLException {
        Connection conn = null;
        PreparedStatement prepStat = null;
        try {
            conn = DataBaseConnectionPool.getConnection();
            Statement statement = conn.createStatement();
            prepStat = conn.prepareStatement("UPDATE TUTOR SET NAME = ?, SURNAME = ?, PASSWORD = ? WHERE TUTOR_ID == ?;");
            prepStat.setString(1, administrator.getName());
            prepStat.setString(2, administrator.getSurname());
            prepStat.setString(3, administrator.getPassword());
            prepStat.setInt(4,administrator.getAdministratorId());

            return prepStat.executeUpdate();
        }finally {
            DataBaseConnectionPool.silentClose(prepStat);
            DataBaseConnectionPool.silentClose(conn);
        }
    }
     public int deleteAdministrator(Administrator administrator) throws SQLException {
        Connection conn = null;
        PreparedStatement prepStat = null;
        try {
            conn = DataBaseConnectionPool.getConnection();
            Statement statement = conn.createStatement();
            prepStat = conn.prepareStatement("DELETE FROM TUTOR WHERE TUTOR_ID = ?");
            prepStat.setInt(1, administrator.getAdministratorId());
            return prepStat.executeUpdate();
        }finally {
            DataBaseConnectionPool.silentClose(prepStat);
            DataBaseConnectionPool.silentClose(conn);
        }

     }
}
